package com.easyfun.healthmagicbox.util;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import android.R.bool;
import android.app.Application;

import com.easyfun.healthmagicbox.HealthBoxApplication;
import com.easyfun.healthmagicbox.R;
import com.easyfun.healthmagicbox.ormlite.HealthInputTypeEnum;

public class VoiceTextToValueData {
	static Application app = HealthBoxApplication.getInstance();

	public class EnumAndName {
		public final HealthInputTypeEnum typeEnum;
		public final String typeName;

		EnumAndName(HealthInputTypeEnum typeEnum, String typeName) {
			this.typeEnum = typeEnum;
			this.typeName = typeName;
		}
	}

	public Map<HealthInputTypeEnum, String> convertTo(String voiceText) {

		Map<Integer, EnumAndName> locToTypeMap = new TreeMap<Integer, EnumAndName>();
		// new Comparator<String>(){
		// public int compare(String obj1,String obj2){
		// //降序排序
		// return obj2.compareTo(obj1);
		// }
		// }

		Integer xueya_gaoya = Integer.valueOf(voiceText.indexOf(app
				.getString(R.string.xueya_gaoya)));
		if (xueya_gaoya > -1)
			locToTypeMap.put(
					xueya_gaoya,
					new EnumAndName(HealthInputTypeEnum.XUEYA_GAOYA, app
							.getString(R.string.xueya_gaoya)));

		Integer xueya_diya = Integer.valueOf(voiceText.indexOf(app
				.getString(R.string.xueya_diya)));
		if (xueya_diya > -1)
			locToTypeMap.put(
					xueya_diya,
					new EnumAndName(HealthInputTypeEnum.XUEYA_DIYA, app
							.getString(R.string.xueya_diya)));

		Integer xueyang = Integer.valueOf(voiceText.indexOf(app
				.getString(R.string.xueyang)));
		if (xueyang > -1)
			locToTypeMap.put(
					xueyang,
					new EnumAndName(HealthInputTypeEnum.XUEYANG, app
							.getString(R.string.xueyang)));

		Integer shengao = Integer.valueOf(voiceText.indexOf(app
				.getString(R.string.shengao)));
		if (shengao > -1)
			locToTypeMap.put(
					shengao,
					new EnumAndName(HealthInputTypeEnum.SHENGAO, app
							.getString(R.string.shengao)));

		Integer tizhong = Integer.valueOf(voiceText.indexOf(app
				.getString(R.string.tizhong)));
		if (tizhong > -1)
			locToTypeMap.put(
					tizhong,
					new EnumAndName(HealthInputTypeEnum.TIZHONG, app
							.getString(R.string.tizhong)));

		Integer xuetang_canhou = voiceText.indexOf(app
				.getString(R.string.xuetang_canhou));
		if (xuetang_canhou > -1)
			locToTypeMap.put(
					xuetang_canhou,
					new EnumAndName(HealthInputTypeEnum.XUETANG_CANHOU, app
							.getString(R.string.xuetang_canhou)));

		Integer xuetang_canqian = voiceText.indexOf(app
				.getString(R.string.xuetang_canqian));
		if (xuetang_canqian > -1)
			locToTypeMap.put(
					xuetang_canqian,
					new EnumAndName(HealthInputTypeEnum.XUETANG_CANQIAN, app
							.getString(R.string.xuetang_canqian)));

		Set<Integer> keySet = locToTypeMap.keySet();
		Iterator<Integer> iter = keySet.iterator();

		Map<HealthInputTypeEnum, String> resultmap = new LinkedHashMap<HealthInputTypeEnum, String>();

		Integer key;
		boolean loopflag = true;
		if (iter.hasNext()) {
			key = iter.next();
			while (loopflag) {
				Integer nextkey = 0;
				if (iter.hasNext()) {
					nextkey = iter.next();
				} else {
					nextkey = voiceText.length();
					loopflag = false;
				}

				EnumAndName enumAndname = locToTypeMap.get(key);
				HealthInputTypeEnum typeEnum = enumAndname.typeEnum;
				String typeName = enumAndname.typeName;
				String value = getValue(voiceText, key, nextkey, typeEnum,
						typeName);
				resultmap.put(typeEnum, value);

				key = nextkey;

			}
		}
		return resultmap;
	}

	public String getValue(String voiceText, int startIndex, int endIndex,
			HealthInputTypeEnum type, String typeName) {
		String text = voiceText.substring(startIndex, endIndex);
		int danweiLoc = 0;
		switch (type) {
		case TIZHONG:
			danweiLoc = text.indexOf(app.getString(R.string.tizhong_gongjin));
			if (danweiLoc > -1) {
				String chnvalue = text.substring(
						typeName.length(), danweiLoc);
				int ret = parseChineseNumber(chnvalue);
				if (ret == -1)
				{
					return Double.valueOf(getDoubleValue(chnvalue)).toString();
				}
				return String.valueOf( ret);
			}
			danweiLoc = text.indexOf(app.getString(R.string.tizhong_jin));
			if (danweiLoc > -1) {
				String chnvalue = text.substring(
						 typeName.length(), danweiLoc);
				return String.valueOf(parseChineseNumber(chnvalue));
			}
			break;
		case SHENGAO:
			danweiLoc = text.indexOf(app.getString(R.string.shengo_mi));
			if (danweiLoc > -1) {
				String chnvalue = text.substring(
						startIndex + typeName.length(), danweiLoc);
				int ret = parseChineseNumber(chnvalue);
				if (ret == -1)
				{
					return Double.valueOf(getDoubleValue(chnvalue)).toString();
				}
				return String.valueOf( ret);
			}
			break;
		case XUETANG_CANHOU:
			break;
		case XUETANG_CANQIAN:
			break;
		case XUEYANG:
			break;
		case XUEYA_DIYA:
		case XUEYA_GAOYA:
			break;
		default:
			break;

		}
		return "";
	}

	/**
	 * 把中文数字解析为阿拉伯数字(Integer)
	 * 
	 * @param chineseNumber
	 *            中文数字
	 * @return 阿拉伯数字(Integer),如果是无法识别的中文数字则返回-1
	 */
	public static int parseChineseNumber(String chineseNumber) {
		chineseNumber = chineseNumber.replace("仟", "千");
		chineseNumber = chineseNumber.replace("佰", "百");
		chineseNumber = chineseNumber.replace("拾", "十");
		chineseNumber = chineseNumber.replace("玖", "九");
		chineseNumber = chineseNumber.replace("捌", "八");
		chineseNumber = chineseNumber.replace("柒", "七");
		chineseNumber = chineseNumber.replace("陆", "六");
		chineseNumber = chineseNumber.replace("伍", "五");
		chineseNumber = chineseNumber.replace("肆", "四");
		chineseNumber = chineseNumber.replace("叁", "三");
		chineseNumber = chineseNumber.replace("贰", "二");
		chineseNumber = chineseNumber.replace("壹", "一");
		return parseChineseNumber(chineseNumber, 1);
	}

	/**
	 * 把中文数字解析为阿拉伯数字(Integer)
	 * 
	 * @param preNumber
	 *            第二大的进位
	 * @param chineseNumber
	 *            中文数字
	 * @return 阿拉伯数字(Integer),如果是无法识别的中文数字则返回-1
	 */
	private static int parseChineseNumber(String chineseNumber, int preNumber) {
		int ret = 0;
		if (chineseNumber.indexOf("零") == 0) {
			int index = 0;
			int end = chineseNumber.length();
			String prefix = chineseNumber.substring(index + 1, end);
			ret = parseChineseNumber(prefix, 1);
		} else if (chineseNumber.indexOf("亿") != -1) {
			int index = chineseNumber.indexOf("亿");
			int end = chineseNumber.length();
			String prefix = chineseNumber.substring(0, index - 1);
			String postfix = chineseNumber.substring(index + 1, end);
			ret = parseChineseNumber(prefix, 1) * 100000000
					+ parseChineseNumber(postfix, 10000000);
		} else if (chineseNumber.indexOf("万") != -1) {
			int index = chineseNumber.indexOf("万");
			int end = chineseNumber.length();
			String prefix = chineseNumber.substring(0, index);
			String postfix = chineseNumber.substring(index + 1, end);
			ret = parseChineseNumber(prefix, 1) * 10000
					+ parseChineseNumber(postfix, 1000);
		} else if (chineseNumber.indexOf("千") != -1) {
			int index = chineseNumber.indexOf("千");
			int end = chineseNumber.length();
			String prefix = chineseNumber.substring(0, index);
			String postfix = chineseNumber.substring(index + 1, end);
			ret = parseChineseNumber(prefix, 1) * 1000
					+ parseChineseNumber(postfix, 100);
		} else if (chineseNumber.indexOf("百") != -1) {
			int index = chineseNumber.indexOf("百");
			int end = chineseNumber.length();
			String prefix = chineseNumber.substring(0, index);
			String postfix = chineseNumber.substring(index + 1, end);
			ret = parseChineseNumber(prefix, 1) * 100
					+ parseChineseNumber(postfix, 10);
		} else if (chineseNumber.indexOf("十") != -1) {
			int index = chineseNumber.indexOf("十");
			int end = chineseNumber.length();
			String prefix = chineseNumber.substring(0, index);
			String postfix = chineseNumber.substring(index + 1, end);
			ret = parseChineseNumber(prefix, 1) * 10
					+ parseChineseNumber(postfix, 1);
		} else if (chineseNumber.equals("一")) {
			ret = 1 * preNumber;
		} else if (chineseNumber.equals("二")) {
			ret = 2 * preNumber;
		} else if (chineseNumber.equals("三")) {
			ret = 3 * preNumber;
		} else if (chineseNumber.equals("四")) {
			ret = 4 * preNumber;
		} else if (chineseNumber.equals("五")) {
			ret = 5 * preNumber;
		} else if (chineseNumber.equals("六")) {
			ret = 6 * preNumber;
		} else if (chineseNumber.equals("七")) {
			ret = 7 * preNumber;
		} else if (chineseNumber.equals("八")) {
			ret = 8 * preNumber;
		} else if (chineseNumber.equals("九")) {
			ret = 9 * preNumber;
		} else if (chineseNumber.equals("")) {
			ret = 0;
		} else {
			ret = -1;
		}
		return ret;
	}

	

		/**
		 * 解析str，获得其中的整数
		 * 
		 * @param str
		 * @return
		 */
		public static int getIntValue(String str) {
			int r = 0;
			if (str != null && str.length() != 0) {
				StringBuffer bf = new StringBuffer();

				char[] chars = str.toCharArray();
				for (int i = 0; i < chars.length; i++) {
					char c = chars[i];
					if (c >= '0' && c <= '9') {
						bf.append(c);
					} else if (c == ',') {
						continue;
					} else {
						if (bf.length() != 0) {
							break;
						}
					}
				}
				try {
					r = Integer.parseInt(bf.toString());
				} catch (Exception e) {
				}
			}
			return r;
		}

		/**
		 * 解析字符串获得双精度型数值，
		 * 
		 * @param str
		 * @return
		 */
		private static double getDoubleValue(String str) {
			double d = 0;

			if (str != null && str.length() != 0) {
				StringBuffer bf = new StringBuffer();

				char[] chars = str.toCharArray();
				for (int i = 0; i < chars.length; i++) {
					char c = chars[i];
					if (c >= '0' && c <= '9') {
						bf.append(c);
					} else if (c == '.') {
						if (bf.length() == 0) {
							continue;
						} else if (bf.indexOf(".") != -1) {
							break;
						} else {
							bf.append(c);
						}
					} else {
						if (bf.length() != 0) {
							break;
						}
					}
				}
				try {
					d = Double.parseDouble(bf.toString());
				} catch (Exception e) {
				}
			}

			return d;
		}
	
}
